#lang racket/gui
;; one of 'hibernate 'sleep or 'lock
(define type (make-parameter 'hibernate))
;; 'unix 'windows 'macosx
(define sys (system-type))
(define time (make-parameter 30))

(define cmd-line
  (command-line
   #:once-each
   [("-t" "--time") t "time till sleep"
                    (time (string->number t))
                    (unless (and (string->number t)
                              (>= (string->number t) 0)) 
                      (error "time requires a number"))]
   [("-m" "--mode") m ("sleep type"
                       "oneof: hibernate sleep lock")
                    (type (string->symbol m))
                    (unless (case (string->symbol m)
                              [(hibernate sleep lock) #t]
                              [else #f])
                      (error "mode requires oneof:  hibernate sleep lock"))]))

(define popup%
  (class frame%
    (define/augment (on-close)
      (exit))
    (super-new [label (string-append "will "
                                     (symbol->string (type))
                                     " soon")])))
(define popup (new popup%))



(define warning (new message%
                     [label (string-append "time: "
                                      (number->string (time)))]
                     [parent popup]))

(define cancel (new button%
                    [label "cancel"]
                    [parent popup]
                    [callback (λ (e b)
                                (exit))]))
(send cancel focus)

(send popup show #t)

(define (type-case hi sl lk)
  (case (type)
    [(hibernate) hi]
    [(sleep) sl]
    [(lock) lk]))

(thread
 (λ ()
   
   (for ([i (in-range (time))])
     (sleep 1)
     (time (- (time) 1))
     (send warning set-label (string-append "time: "
                                            (number->string (time)))))
   
   (system 
    (case sys
      [(macosx) (type-case "hibernate" "sleep" "lock")]
      [(unix)
       (type-case "sh /etc/acpi/hibernate.sh" "sh /etc/acpi/sleepbtn.sh" "gnome-screensaver-command –lock")]
      [(windows)
       (type-case "shutdown -h -f" "rundll32.exe user32.dll, LockWorkStation" "rundll32.exe user32.dll, LockWorkStation")]))
   (exit)))



